package cn.yedong.org.controller;


import cn.yedong.basic.util.AjaxResult;
import cn.yedong.basic.util.ExcelUtils;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.org.domain.Shop;
import cn.yedong.org.domain.ShopAuditLog;
import cn.yedong.org.query.ShopQuery;
import cn.yedong.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
@Api(value="店铺接口",description = "处理店铺的操作")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @ApiOperation("接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @ApiOperation("接口：删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @ApiOperation("接口：批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
                shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @ApiOperation("接口：查询单个对象")
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @ApiOperation("接口：查询所有")
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @ApiOperation("接口：分页查询或高级查询")
    @PostMapping
    public QueryResult<Shop> findPage(@RequestBody ShopQuery query) {
        return shopService.pageQuery(query);
    }

    /**
     * 接口：店铺注册
     * @param shop
     * @return
     */
    @ApiOperation("接口：分页查询或高级查询")
    @PostMapping("/settlement")
    public AjaxResult register(@RequestBody Shop shop){
        try {
            shopService.register(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 接口：审核通过
     * @param shopAuditLog
     * @return
     */
    @RequestMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 接口：审核失败
     * @param shopAuditLog
     * @return
     */
    @RequestMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败！");
        }
    }

    /**
     * 数据导出
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName,HttpServletResponse response
        ExcelUtils.exportExcel(shops,null,"店铺数据",Shop.class,"系统_店铺数据.xls",response);
    }

    /**
     * 数据导出
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        //ExcelUtils.importExcel(file, 0, 1, Shop.class);
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(res-> System.out.println(res));
    }

    /**
     * 店铺激活
     * @return
     */
    @GetMapping("/activate/{shopId}")
    public String activate(@PathVariable("shopId") Long shopId){
        try {
            shopService.activate(shopId);
            return "<h1>恭喜你，店铺注册成功！</h1><a href='http://127.0.0.1:8081/#/login'>点击跳转店铺登陆</a>";
        } catch (Exception e) {
            e.printStackTrace();
            return "网络异常，请稍后重试！";
        }
    }
}
